<?php

namespace XWAM\Addon\POrgCustomer\Controller;

/**
 * 系统扩展字段控制器
 *
 * @author Alen
 * @since 1.0 <2014-11-11> by sutroon, 
 * @since 2.0 <2014-12-12> by sutroon
 */
class CustomerAttrController extends AddonbaseController {

    public function attr_list() {
        AddonD('CustomerAttr')->generate_default_fields();
        AddonD('CustomerAttr')->where(array('is_listable'))->getField('field_name, title');

        $list = AddonD('CustomerAttr')->get_paging_list(null, $show, array('ORDERBY' => 'sort, id'));
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->display_cpp();
    }

    // 新增 2014-12-12 by sutroon
    public function attr_add() {
        $this->display();
    }

    // 编辑 2014-11-11 by sutroon
    public function attr_edit($id = 0) {
        $data = AddonD('CustomerAttr')->find($id);
        $this->assign('data', $data);
        $this->display();
    }

    // 保存新增的数据 2014-12-12 by sutroon
    public function attr_add_save() {
        $result = AddonD('CustomerAttr')->add_attr();
        if ($result['status']) {
            $this->success('保存成功', AddonU('attr_list'));
        } else {
            $this->error($result['info']);
        }
    }

    // 保存编辑的数据 2014-11-11 by sutroon
    public function attr_edit_save() {
        $result = AddonD('CustomerAttr')->save_attr();
        $this->dialogJump($result['status'], $result['info']);
    }

    function attr_delete($id = '') {
        $this->ajaxReturn(AddonD('CustomerAttr')->delete_attr($id));
    }

    // 检测字段是否存在 2014-11-11 by sutroon
    public function check_attr_name($name) {
        $result = AddonD('CustomerAttr')->check_attr_name($name);
        $this->ajaxReturn($result);
    }

    // 检测字段是否存在 2014-11-11 by sutroon
    public function check_attr_title($title) {
        $result = AddonD('CustomerAttr')->check_attr_title($title);
        $this->ajaxReturn($result);
    }

    function change_status($id, $status, $type = '') {
        if ('status' != $type) {
            $type = 'is_' . $type;
        }
        $this->ajaxReturn(AddonD('CustomerAttr')->change_status($id, $type, $status));
    }

    function change_sort($id, $sort) {
        $this->ajaxReturn(AddonD('CustomerAttr')->change_sort($id, $sort));
    }

}
